#include "rewine.h"

#include "interop/ImageHandler.hpp"
#include "rewine/LoaderSingleton.hpp"

#include <memory>
#include <string>

using rewine::IExport;
using rewine::IImage;
using std::shared_ptr;
using std::string;

int
rewine_GetExportCount(rw_image_t handle) {
    auto pImage = rewine::HandleToPImage(handle);
    if (!pImage) {
        return -1;
    }
    return pImage->getExportCount();
}

void *
rewine_GetExportByIndex(rw_image_t handle, unsigned int index) {
    auto pImage = rewine::HandleToPImage(handle);
    if (!pImage) {
        return nullptr;
    }
    auto pExport = pImage->getExportByIndex(index);
    return pExport->getValue();
}

void *
rewine_GetExportByName(rw_image_t handle, const char *symbol, uint16_t hint) {
    auto pImage = rewine::HandleToPImage(handle);
    if (!pImage) {
        return nullptr;
    }
    auto pExport = pImage->getExportByName(string(symbol), hint);
    return pExport->getValue();
}

void *
rewine_ResolveSymbol(const char *dll, const char *symbol, uint16_t hint) {
    string sDll(dll); lower_string(sDll);
    auto pLib = rewine::g_Loader->getLibManager().get(sDll);
    auto pExport = pLib->resolve(symbol, hint, nullptr);
    if (!pExport) {
        return nullptr;
    }
    return pExport->getValue();
}
